class Stack {
    constructor () {
      this.count = 0
      this.items = {}
    }
    push (element) {
      this.items[this.count] = element
      this.count++
    }
    size () {
      return this.count
    }
    isEmpty () {
      return this.count === 0
    }
    pop () {
      if (this.isEmpty()) {
        return undefined
      }
      const result = this.items[this.count--]
      delete this.items[this.count]
      return result
    }
    peek () {
      return this.items[this.count - 1]
    }
    clear () {
      this.count = 0
      this.items = {}
    }
    toString () {
      if (this.isEmpty()) {
        return ''
      }
      let result = this.items['0']
      for(let i = 1; i < this.count; i++) {
        result = `${result},${this.items[i]}`
      }
      return result
    }
}

function dec2bin(dec) {
    // new 一个 Stack，保存余数
    const stack = new Stack();

    // 当不确定循环次数时，使用 while 循环
    while (dec > 0) {
      // 除二取余法
      stack.push(dec % 2); // 获取余数，放入栈中
      dec = Math.floor(dec / 2); // 除数除以二，向下取整
    }

    let binaryString = "";
    // 不断地从栈中取出元素（0 或 1），并拼接到一起。
    while (!stack.isEmpty()) {
      binaryString += stack.pop();
    }

    return binaryString;
}

// dec2bin() 测试
console.log(dec2bin(100)); //--> 1100100
console.log(dec2bin(88)); //--> 1011000